const mysql = require('mysql');

async function pro (sql){
    let pro = await new Promise((resolved,reject)=>{
        var con = mysql.createConnection({
            user:'root',
            // password:'PoralMall123...',
            password:'admin123...',
            database:'poral-mall',
            // host: '47.104.24.155',
            host: 'localhost',
            port: 3306
        })
        con.connect();
        
        con.query(sql,(error,result)=>{
            if(error){
                console.log(error);
                resolved(status(0,error,[]))
            }else{
                resolved(status(1,'',result))
            }
        })
    })
    return pro
}


function status(code=1,msg='',result=[]){
    return {
        code,
        msg,
        result
    }
}

function str_status(obj,msg='',result=[]){
    if(typeof obj == 'object'){
        return JSON.stringify(obj)
    }else{
        return JSON.stringify({
            code: obj,
            msg,
            result
        })
    }
    
}

function timeStr(){
    // let data = new Date();
    // // 当前时间戳
    // let timeStemp = data.getTime();
    // // 与周一相差几天
    // let day = data.getDay()-1;
    // // 周一的时间戳
    // let s_timeStemp =timeStemp - day * 24 * 60 * 60 * 1000;
    // let s_data = new Date(s_timeStemp);
    // let s_time = new Date(`${s_data.getFullYear()}-${s_data.getMonth()+1}-${s_data.getDate()} 00:00:00:000`).getTime();
    // // 周日的时间戳
    // let e_day = 7-data.getDay();
    // let e_timeStemp = timeStemp + e_day * 24 * 60 * 60 * 1000;
    // let e_data = new Date(e_timeStemp);
    // let e_time = new Date(`${e_data.getFullYear()}-${e_data.getMonth()+1}-${e_data.getDate()} 23:59:59:999`).getTime();
    return {
        s_time:1675267200000,
        e_time: 1703260800000
    }
}

async function http(url){
    let pro = new Promise((resolved,reject) => {
        let axios = require('axios');
        axios({
            method: 'get',
            url
        }).then(res => {
            resolved(res.data)
        }).catch(error => {
            resolved(status(0,'失败',error))
        })
        
    })
    return pro
}
module.exports = {
    pro,
    status,
    str_status,
    timeStr,
    http
};